package com.itheima.restkeeper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.restkeeper.basic.BasicPojo;
import com.itheima.restkeeper.constant.SuperConstant;
import com.itheima.restkeeper.mapper.BrandMapper;
import com.itheima.restkeeper.pojo.Brand;
import com.itheima.restkeeper.req.BrandVo;
import com.itheima.restkeeper.service.IBrandService;
import com.itheima.restkeeper.utils.BeanConv;
import com.itheima.restkeeper.utils.EmptyUtil;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Description：品牌管理 服务实现类
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService {

    /**
     * 分页查询业务层实现
     * @param brandVo 查询条件
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return
     */
    @Override
    public Page<Brand> findBrandVoPage(BrandVo brandVo, int pageNum, int pageSize) {
        //构建分页对象
        Page<Brand> brandPage = new Page<>(pageNum,pageSize);
        //构建查询条件
        LambdaQueryWrapper<Brand> brandQueryWrapper = Wrappers.<Brand>lambdaQuery();
        //按品牌名称查询
        brandQueryWrapper.eq(!EmptyUtil.isNullOrEmpty(brandVo.getBrandName()),Brand::getBrandName,brandVo.getBrandName());
        //按品牌分类查询
        brandQueryWrapper.eq(!EmptyUtil.isNullOrEmpty(brandVo.getCategory()),Brand::getCategory,brandVo.getCategory());
        //按品牌状态查询
        brandQueryWrapper.eq(!EmptyUtil.isNullOrEmpty(brandVo.getEnableFlag()),BasicPojo::getEnableFlag,brandVo.getEnableFlag());
        //按创建时间降序
        brandQueryWrapper.orderByDesc(BasicPojo::getCreatedTime);
        //执行分页查询
        return page(brandPage,brandQueryWrapper);
    }

    /**
     * 新增品牌业务层实现
     * @param brandVo 对象信息
     * @return
     */
    @Override
    public Brand createBrand(BrandVo brandVo) {
        //转换BrandVo为Brand
        Brand brand = BeanConv.toBean(brandVo, Brand.class);
        if (EmptyUtil.isNullOrEmpty(brand)) {
            throw new RuntimeException("传入品牌有误,或转换出现错误");
        }
        //执行保存
        boolean save = this.save(brand);
        if (!save) {
            throw new RuntimeException("新增品牌出现错误");
        }
        return brand;
    }

    /**
     * 修改品牌
     * @param brandVo 对象信息
     * @return
     */
    @Override
    public Boolean updateBrand(BrandVo brandVo) {
        //转换BrandVo为Brand
        Brand brand = BeanConv.toBean(brandVo, Brand.class);
        //按ID执行修改
        return this.updateById(brand);
    }


    /**
     * 根据点选节点删除品牌
     * @param checkedIds 选择中对象Ids
     * @return
     */
    @Override
    public Boolean deleteBrand(String[] checkedIds) {
        //转换数组为集合
        List<String> strings = Arrays.asList(checkedIds);
        //批量删除
        boolean b = this.removeByIds(strings);
        return b;
    }

    /**
     * 查询品牌下拉框
     * @return
     */
    @Override
    public List<Brand> findBrandVoList() {
        //构建查询条件
        LambdaQueryWrapper<Brand> lambdaQueryWrapper =Wrappers.<Brand>lambdaQuery();
        //查询有效状态
        lambdaQueryWrapper.eq(BasicPojo::getEnableFlag, SuperConstant.YES);
        return list(lambdaQueryWrapper);
    }
}
